#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_logic( 0 )
{
    ui->setupUi(this);

    connect( ui->actionNew, SIGNAL( triggered() ), this, SLOT( createNewGame() ) );
    connect( ui->actionSolve, SIGNAL( triggered() ), this, SLOT( solveGame() ) );
    connect( ui->actionManual, SIGNAL( triggered() ), this, SLOT( showManual() ) );
    connect( ui->actionSolve, SIGNAL( triggered() ), this, SLOT( solveGame() ) );
    connect( ui->actionExit, SIGNAL( triggered() ), this, SLOT( close() ) );

    connect( ui->actionEasy, SIGNAL( triggered() ), this, SLOT( setEasy() ) );
    connect( ui->actionMedium, SIGNAL( triggered() ), this, SLOT( setMedium() ) );
    connect( ui->actionHard, SIGNAL( triggered() ), this, SLOT( setHard() ) );

    connect( ui->pushButton_help, SIGNAL( pressed() ), this, SLOT( help() ) );
    connect( ui->pushButton_check, SIGNAL( pressed() ), this, SLOT( check() ) );
    connect( ui->pushButton_pause, SIGNAL( pressed() ), this, SLOT( pause() ) );

    for( int  i = 0; i < 9; ++i )
    {
        GridWidget* grid = new GridWidget( this );
        m_gridList.append( grid );
        ui->gridLayout->addWidget( grid, i/3, i%3 );
    }

    setWindowIcon(QIcon("icon.png"));

    ui->widget_game->setDisabled( true );
    ui->pushButton_check->setDisabled( true );
    ui->pushButton_help->setDisabled( true );
    ui->pushButton_pause->setDisabled( true );

    ui->actionSolve->setDisabled( true );

    setEasy();

    m_timer = new Timer( ui->lcdNumber );
}

MainWindow::~MainWindow()
{
    delete ui;

    qDeleteAll( m_gridList );
    m_gridList.clear();

    if( m_logic )
        delete m_logic;

    delete m_timer;
}

void MainWindow::createNewGame()
{
    if( m_logic )
        delete m_logic;

    if( ui->actionEasy->isChecked() )
        m_logic = new Logic( 0 );
    else if( ui->actionMedium->isChecked() )
        m_logic = new Logic( 1 );
    else if( ui->actionHard->isChecked() )
        m_logic = new Logic( 2 );

    for( int row = 0; row < 9; ++row )
        for( int column = 0; column < 9; ++column )
        {
            if( m_logic->getNumberReady( row, column) == m_logic->getNumberUser( row, column))
            {
                QString num = QString::number( m_logic->getNumberReady( row, column) );
                m_gridList.at( row )->setValue( column/3, column%3, num );
                const QString styleSheet( "color:black; border:1px solid gray; border-radius:5px; border-color:#808080; border-width:1px; padding:2; background-color:#E6E6FA;" );
                m_gridList.at( row )->setStyleSheet( column/3, column%3, styleSheet );
                m_gridList.at( row )->setLineEditReadOnly( column/3, column%3 );
            }
            else
            {
                m_gridList.at( row )->setValue( column/3, column%3 );
                const QString styleSheet( "border:1px solid gray; border-radius:5px; border-color:Gray; border-width:1px; padding:2; background-color:#F0F8FF;" );
                m_gridList.at( row )->setStyleSheet( column/3, column%3, styleSheet );
                m_gridList.at( row )->setLineEditReadOnly( column/3, column%3, false );
            }
        }

    ui->widget_game->setDisabled( false );
    ui->pushButton_check->setDisabled( false ); // TODO
    ui->pushButton_help->setDisabled( false );
    ui->pushButton_pause->setDisabled( false );

    ui->actionSolve->setDisabled( false );

    m_timer->resetTimer();
    m_timer->startTimer();
}

void MainWindow::solveGame()
{
    for( int row = 0; row < 9; ++row )
        for( int column = 0; column < 9; ++column )
        {
            if( !m_gridList.at( row)->getLineEditReadOnly(column/3, column%3) )
            {
                QString num = QString::number( m_logic->getNumberReady( row, column) );
                m_gridList.at( row )->setValue( column/3, column%3, num );
                m_gridList.at( row )->setLineEditReadOnly( column/3, column%3 );
                const QString styleSheet( "border:1px solid gray; border-radius:5px; border-color:#808080; border-width:1px; padding:2; background-color:#F0F8FF; color:green;selection-background-color: darkblue ;" );
                m_gridList.at( row )->setStyleSheet( column/3, column%3, styleSheet );
            }
        }

    m_timer->stopTimer();
    ui->actionSolve->setDisabled(true);
    ui->pushButton_pause->setDisabled(true);
    ui->pushButton_help->setDisabled(true);
    ui->pushButton_check->setDisabled(true);
}

void MainWindow::pause()
{
    if( ui->actionNew->isEnabled() )// Deaktivierung
     {
        for( int i = 0; i < 9; ++i )
            m_gridList.at(i)->hide();

         ui->pushButton_pause->setText("Resume");
         ui->pushButton_help->setDisabled(true);
         ui->pushButton_check->setDisabled(true);
         ui->actionNew->setDisabled(true);
         ui->actionSolve->setDisabled(true);
         m_timer->stopTimer();
     }
     else                       // Aktivierung
     {
        for( int i = 0; i < 9; ++i )
            m_gridList.at(i)->show();

         ui->pushButton_pause->setText("Pause");
         ui->pushButton_help->setDisabled(false);
         ui->pushButton_check->setDisabled(false);
         ui->actionNew->setDisabled(false);
         ui->actionSolve->setDisabled(false);
         m_timer->startTimer();
     }
}

void MainWindow::check()
{
    for( int row = 0; row < 9; ++row )
        for( int column = 0; column < 9; ++column )
        {
            if( !m_gridList.at( row)->getLineEditReadOnly(column/3, column%3) )
            {
                int value = m_gridList.at( row)->getLineEditValue(column/3, column%3);
                if( value != 0 )
                {
                    if( m_logic->getNumberReady(row, column) == value )
                    {
                        QString num = QString::number( m_logic->getNumberReady( row, column) );
                        m_gridList.at( row )->setValue( column/3, column%3, num );
                        m_gridList.at( row )->setLineEditReadOnly( column/3, column%3 );
                        const QString styleSheet( "border:1px solid gray; border-radius:5px; border-color:#808080; border-width:1px; padding:2; background-color:#F0F8FF; color:green;selection-background-color: darkblue ;" );
                        m_gridList.at( row )->setStyleSheet( column/3, column%3, styleSheet );
                    }
                    else
                    {
                        m_gridList.at( row )->setLineEditReadOnly( column/3, column%3, false );
                        const QString styleSheet( "border:1px solid gray; border-radius:5px; border-color:#808080;border-width:1px; padding:2; background-color:#F0F8FF; color:red;selection-background-color: darkblue ;" );
                        m_gridList.at( row )->setStyleSheet( column/3, column%3, styleSheet );
                    }
                }
            }
        }

    if(  m_logic->isFinished() )
        isWon();
}

void MainWindow::help()
{
    for( int row = 0; row < 9; ++row )
        for( int column = 0; column < 9; ++column )
        {
            if( !m_gridList.at( row)->getLineEditReadOnly(column/3, column%3) )
            {
                int value = m_gridList.at( row)->getLineEditValue(column/3, column%3);
                if( value != 0 )
                {
                    QString num = QString::number( m_logic->getNumberReady( row, column) );
                    m_gridList.at( row )->setValue( column/3, column%3, num );
                    m_gridList.at( row )->setLineEditReadOnly( column/3, column%3 );
                    const QString styleSheet( "border:1px solid gray; border-radius:5px; border-color:#808080; border-width:1px; padding:2; background-color:#F0F8FF; color:green;selection-background-color: darkblue ;" );
                    m_gridList.at( row )->setStyleSheet( column/3, column%3, styleSheet );

                    if(  m_logic->isFinished() )
                        isWon();

                    return;
                }
            }
        }


    while( true )
    {
        int row = rand()%9;
        int column = rand()%9;

        if( m_logic->getNumberUser( row, column) == 0 )
        {
            int value = m_logic->getNumberReady( row, column);
            QString num = QString::number( value );
            m_gridList.at( row )->setValue( column/3, column%3, num );
            m_gridList.at( row )->setLineEditReadOnly( column/3, column%3 );
            const QString styleSheet( "border:1px solid gray; border-radius:5px; border-color:#808080; border-width:1px; padding:2; background-color:#F0F8FF; color:green;selection-background-color: darkblue ;" );
            m_gridList.at( row )->setStyleSheet( column/3, column%3, styleSheet );
            m_logic->setNumberUser( row, column, value );

            if(  m_logic->isFinished() )
                isWon();

            return;
        }
    }
}

void MainWindow::setEasy()
{
    ui->actionEasy->setChecked( true );
    ui->actionMedium->setChecked( false );
    ui->actionHard->setChecked( false );
}

void MainWindow::setMedium()
{
    ui->actionEasy->setChecked( false );
    ui->actionMedium->setChecked( true );
    ui->actionHard->setChecked( false );
}

void MainWindow::setHard()
{
    ui->actionEasy->setChecked( false );
    ui->actionMedium->setChecked( false );
    ui->actionHard->setChecked( true );
}

void MainWindow::showManual()
{
    QMessageBox::about(this, tr("How to play Sudoku?"),
            tr("<i>How to play sudoku?</i> <br><br>"
                "Solving a sudoku puzzle can be rather tricky, but the rules of the game are quite simple.<br><br>"
                "A sudoku puzzle is a grid of nine by nine squares or cells, that has been<br>"
                "subdivided into nine subgrids or 'regions' of three by three cells.<br>"
                "The objective of sudoku is to enter a digit from 1 through 9 in each cell, in such a way that:<br><br>"
                "<b>•	Each horizontal row contains each digit exactly once<br></b>"
                "<b>•	Each vertical column contains each digit exactly once<br></b>"
                "<b>•	Each subgrid or region contains each digit exactly once<br><br></b>"
                "This explains the name of the game: in Japanese, sudokumeans something like 'numbers singly'.<br><br>"
                "Solving a sudoku puzzle does not require knowledge of mathematics; simple logic suffices.<br>"
                "The puzzler's job is to fill the remainder of the grid with digits –respecting, of course,<br>"
                "the three constraints mentioned earlier.<br><br>"
                "Some functions of this puzzle:<br><br>"
                "<b>•	File -> New:</b>  The game starts<br>"
                "<b>•	File -> Solve:</b>  Solve the game<br>"
                "<b>•	File -> Exit:</b>  Ending the game<br>"
                "<b>•	Edit -> Level:</b>  Select difficulty<br><br>"
                "This sudoku puzzle has only one solution.<br><br>"
             "<b>Good Luck!</b>"
              "<hr>"
              "<b>Developer: </b><br>"
              "Olexandr Shamray<br>"
              "<hr>"
              "Build details: <br>"
              "QT Version: 4.7.2 "
            ));
}

void MainWindow::isWon()
{
    m_timer->stopTimer();  // Zeit gestoppt
    QMessageBox msgBox;
    msgBox.setText("<b> Congratulation! You've done it! </b>");
    msgBox.setStandardButtons(QMessageBox::Close);
    msgBox.setWindowIconText("You've won!");
    msgBox.exec();

    // Buttonsdeaktivierung
    ui->actionSolve->setDisabled(true);
    ui->pushButton_pause->setDisabled(true);
    ui->pushButton_help->setDisabled(true);
    ui->pushButton_check->setDisabled(true);
}
